package com.spzx.product.controller;

/**
 * @Author GQS @Date 2024/9/20/21:14 @Description
 */
import com.github.pagehelper.PageHelper;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.*;
import com.spzx.product.api.domain.vo.SkuLockVo;
import com.spzx.product.api.domain.vo.SkuStockVo;
import com.spzx.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/** 商品Controller */
@Tag(name = "商品接口管理")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

  @Autowired private ProductService productService;


  @InnerAuth
  @Operation(summary = "检查与锁定库存")
  @PostMapping("checkAndLock/{orderNo}")
  public R<String> checkAndLock(@PathVariable String orderNo, @RequestBody List<SkuLockVo> skuLockVoList) {
    try {
      return R.ok(productService.checkAndLock(orderNo, skuLockVoList));
    } catch (ServiceException e) {
      e.printStackTrace();
      return R.ok(e.getMessage());
    } catch (Exception e) {
      e.printStackTrace();
      return R.ok("库存不足");
    }
  }
  @Operation(summary = "批量获取商品sku最新价格信息")
  @InnerAuth
  @PostMapping(value = "/getSkuPriceList")
  public R<List<SkuPrice>> getSkuPriceList(@RequestBody List<Long> skuIdList)
  {
    return R.ok(productService.getSkuPriceList(skuIdList));
  }
  @Operation(summary = "获取商品sku信息")
  @InnerAuth
  @GetMapping(value = "/getProductSku/{skuId}")
  public R<ProductSku> getProductSku(@PathVariable("skuId") Long skuId)
  {
    return R.ok(productService.getProductSku(skuId));
  }

  @Operation(summary = "获取商品信息")
  @InnerAuth
  @GetMapping(value = "/getProduct/{id}")
  public R<Product> getProduct(@PathVariable("id") Long id)
  {
    return R.ok(productService.getProduct(id));
  }


  @Operation(summary = "获取商品sku最新价格信息")
  @InnerAuth
  @GetMapping(value = "/getSkuPrice/{skuId}")
  public R<SkuPrice> getSkuPrice(@PathVariable("skuId") Long skuId)
  {
    return R.ok(productService.getSkuPrice(skuId));
  }

  @Operation(summary = "获取商品详细信息")
  @InnerAuth
  @GetMapping(value = "/getProductDetails/{id}")
  public R<ProductDetails> getProductDetails(@PathVariable("id") Long id)
  {
    return R.ok(productService.getProductDetails(id));
  }
  /**
   * 处理获取SKU列表请求
   */
  @InnerAuth
  @GetMapping("/skuList/{pageNum}/{pageSize}")
  public R<TableDataInfo> skuList(
          @Parameter(name = "pageNum", description = "当前页码", required = true)
          @PathVariable Integer pageNum,

          @Parameter(name = "pageSize", description = "每页记录数", required = true)
          @PathVariable Integer pageSize,

          @Parameter(name = "productQuery", description = "查询对象", required = false)
          @ModelAttribute SkuQuery skuQuery)
  {
    PageHelper.startPage(pageNum, pageSize);
    List<ProductSku> list = productService.selectProductSkuList(skuQuery);
    return R.ok(getDataTable(list));
  }

  /**
   * 处理获取销量好的SKU请求
   * @return 包含销量好SKU列表的响应
   */
  @InnerAuth
  @Operation(summary = "获取销量好的sku")
  @GetMapping("getTopSale")
  public R<List<ProductSku>> getTopSale() {
      return R.ok(productService.getTopSale());
  }

  @Operation(summary = "获取商品sku规则详细信息")
  @InnerAuth
  @GetMapping(value = "/getSkuSpecValue/{id}")
  public R<Map<String, Long>> getSkuSpecValue(@PathVariable("id") Long id)
  {
    return R.ok(productService.getSkuSpecValue(id));
  }

  @Operation(summary = "获取商品sku库存信息")
  @InnerAuth
  @GetMapping(value = "/getSkuStock/{skuId}")
  public R<SkuStockVo> getSkuStock(@PathVariable("skuId") Long skuId)
  {
    return R.ok(productService.getSkuStock(skuId));
  }
// =========================================================================================================================================
  /** 查询商品列表 */
  @Operation(summary = "查询商品列表")
  @GetMapping("/list")
  public TableDataInfo list(Product product
          // , @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize
  ) {
    // startPage()内部调用servletUtils从请求中获取分页参数，并设置到分页插件中
    startPage();
    // PageHelper.startPage(pageNum, pageSize);
    List<Product> list = productService.selectProductList(product);
    return getDataTable(list);
  }

  /**
   * 新增商品
   *
   * @param product
   * @return
   */
  @Operation(summary = "新增商品")
  @PostMapping
  public AjaxResult add(@RequestBody Product product) {
    return toAjax(productService.insertProduct(product));
  }

  /**
   * 获取商品详细信息
   */
  @Operation(summary = "获取商品详细信息")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Long id) {
    return success(productService.selectProductById(id));
  }

  /**
   * 修改商品
   * @param product
   * @return
   */
  @Operation(summary = "修改商品")
  @PutMapping
  public AjaxResult edit(@RequestBody Product product) {
    return toAjax(productService.updateProduct(product));
  }

  /**
   * 删除商品
   *
   * @param ids
   * @return
   */
  @Operation(summary = "删除商品")
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable Long[] ids) {
    return toAjax(productService.deleteProductByIds(ids));
  }

  /**
   * 商品审核
   *
   * @param id
   * @param auditStatus
   * @return
   */
  @Operation(summary = "商品审核")
  @GetMapping("updateAuditStatus/{id}/{auditStatus}")
  public AjaxResult updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
    productService.updateAuditStatus(id, auditStatus);
    return success();
  }

  /**
   * 商品审核
   *
   * @param id
   * @param status
   * @return
   */
  @Operation(summary = "更新上下架状态")
  @GetMapping("updateStatus/{id}/{status}")
  public AjaxResult updateStatus(@PathVariable Long id, @PathVariable Integer status) {
    productService.updateStatus(id, status);
    return success();
  }

}
